# uncompyle6 version 3.2.3
# Python bytecode 3.6 (3379)
# Decompiled from: Python 3.6.8 |Anaconda custom (64-bit)| (default, Feb 21 2019, 18:30:04) [MSC v.1916 64 bit (AMD64)]
# Embedded file name: api\handlers\scan\worker.py
__author__ = "sanyi"
import logging, os.path, base64
from scanners.scan_app.scan_app_base import *
from server.http_responses.http_status_codes import Http204
from settings_provider import settings
from helpers.licensing import get_local_license
from helpers.hashing import md5
from db.tables.workers import WorkerAuthorization
from api.handlers.scan import ScanApiHandler
from helpers.setup_types import is_setup_type_on_premise_worker

_logger = logging.getLogger("api.handlers.scan.worker")


class WorkerApiHandler(ScanApiHandler):
    def get_worker_status(self, **_):
        """
        Returns the standard worker app status extended with license data and worker status
        :param _:
        :return:
        """
        result = self.app.get_worker_status()
        if is_setup_type_on_premise_worker():
            lic = get_local_license()["license_key"]
            if lic:
                lic = md5(lic)
            result.license_hash = lic
            result.authorization = self.settings_storage.get("authorization", "none")
            result.app_version = settings.get("version_info", {}).get("str", "12.0.0")
        return result

    def shutdown(self):
        return self.app.shutdown()

    def check_worker_connection(self, **_):
        """
        used my the master to check if worker is connectible, returns scanning app
        :param _:
        :return:
        """
        return ClsWorkerConnectionInfo(scanning_app=self.app.scanning_app)

    def activate_worker(self, body, **_):
        """
        The master accepts the worker and may send a new license
        :param body:
        :param _:
        :return:
        """
        activation_data = ClsWorkerActivationData(**body)
        if activation_data:
            activation_data = base64.b64decode(activation_data.activation_data)
            open(os.path.join(settings["storage.license"], "wa_data.dat"), "wb").write(
                activation_data
            )
        license_data = get_local_license(True)
        _logger.info(
            "activating worker with data: %s, key: %s",
            bool(activation_data),
            license_data["license_key"],
        )
        if license_data["license_key"]:
            self.settings_storage.set("authorization", WorkerAuthorization.AUTHORIZED)
        return self.get_worker_status()

    def deactivate_worker(self, **_):
        """
        The master notice's the worker that from now one is unlinked and should forget the license
        :param _:
        :return:
        """
        self.settings_storage.delete("master_endpoint")
        return Http204()

    def add_licence(self, body, **_):
        """
        The master sends a new license to the worker
        :param body:
        :param _:
        :return:
        """
        return self.activate_worker(body)
